package com.ruoyi.develop.goodsSaleMaterialDetail.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.IGoodsOrderInService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaleMaterial.domain.GoodsSaleMaterial;
import com.ruoyi.develop.goodsSaleMaterial.service.IGoodsSaleMaterialService;
import com.ruoyi.develop.goodsSaleMaterialChild.domain.GoodsSaleMaterialChild;
import com.ruoyi.develop.goodsSaleMaterialChild.service.IGoodsSaleMaterialChildService;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import com.ruoyi.develop.lvRukumingxi.service.impl.LvRukumingxiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSaleMaterialDetail.mapper.GoodsSaleMaterialDetailMapper;
import com.ruoyi.develop.goodsSaleMaterialDetail.domain.GoodsSaleMaterialDetail;
import com.ruoyi.develop.goodsSaleMaterialDetail.service.IGoodsSaleMaterialDetailService;

/**
 * 销售产品物料选用明细Service业务层处理
 * @author qh
 * @date 2025-01-04
 */
@Service
public class GoodsSaleMaterialDetailServiceImpl implements IGoodsSaleMaterialDetailService {
    @Resource
    private GoodsSaleMaterialDetailMapper goodsSaleMaterialDetailMapper;
    @Resource
    private IGoodsSaleMaterialDetailService iGoodsSaleMaterialDetailService;
    @Resource
    private IGoodsSaleMaterialService iGoodsSaleMaterialService;
    @Resource
    private LvRukumingxiServiceImpl lvRukumingxiService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private IGoodsOrderInService goodsOrderInService;
    @Autowired
    private IFactoryProduceSaleAllService iFactoryProduceSaleAllService;
    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;
    @Resource
    private IGoodsSaleMaterialChildService iGoodsSaleMaterialChildService;
    /**
     * 查询销售产品物料选用明细
     * @param id 销售产品物料选用明细主键
     * @return 销售产品物料选用明细
     */
    @Override
    public GoodsSaleMaterialDetail selectById(String id) {
        return goodsSaleMaterialDetailMapper.selectById(id);
    }

    /**
     * 查询销售产品物料选用明细列表
     * @param entity 销售产品物料选用明细
     * @return 销售产品物料选用明细
     */
    @Override
    public List<GoodsSaleMaterialDetail> selectList(GoodsSaleMaterialDetail entity) {
        return goodsSaleMaterialDetailMapper.selectList(entity);
    }

    /**
     * 新增销售产品物料选用明细
     * @param entity 销售产品物料选用明细
     * @return 结果
     */
    @Override
    public int insert(GoodsSaleMaterialDetail entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsSaleMaterialDetailMapper.insert(entity);
    }

    //工人领料
    @Override
    public int submit(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        BigDecimal usedCount=BigDecimal.ZERO;
        if (!rows.isEmpty()) {
            GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(entity.getMaterialId());
            FactoryProduceSaleAll goodsSale=iFactoryProduceSaleAllService.selectById(entity.getSaleAllId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());
                if(goodsSaleMaterial!=null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String time = sdf.format(new Date());
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                    LvRukumingxi lvRukumingxiChuku=new LvRukumingxi();
                    BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiChuku, "id");
                    lvRukumingxiChuku.setMingxiId(lvRukumingxi.getId());
                    lvRukumingxiChuku.setDaohuoCount(lvRukumingxi.getUseCount());
                    lvRukumingxiChuku.setCanCount(BigDecimal.ZERO);
                    lvRukumingxiChuku.setRukudate(new Date());
                    lvRukumingxiChuku.setRukuno(time);
                    lvRukumingxiChuku.setRukuren(SecurityUtils.getLoginUser().getUsername());
                    lvRukumingxiChuku.setType(BusinessConstants.TYPR_CHUKU);
                    lvRukumingxiChuku.setFactoryGoodssaleId(entity.getSaleAllId());
                    lvRukumingxiChuku.setFactoryProdNo(entity.getProduceNo());
                    lvRukumingxiChuku.setFactoryProdId(entity.getProduceId());
                    if(goodsSale!=null){
                        lvRukumingxiChuku.setProjectId(goodsSale.getProjectId());
                        lvRukumingxiChuku.setProjectNo(goodsSale.getProjectNo());
                        lvRukumingxiChuku.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    if(lvRukumingxi.getGoodsBuyTotal()!=null){
                        LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
                        lvRukumingxiChuku.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().subtract(lvRukumingxi.getGoodsBuyTotal()));
                    }
                    lvRukumingxiService.insert(lvRukumingxiChuku);
                    GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(lvRukumingxiChuku.getGoodsId());
                    goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderInService.update(goodsOrderIn);

                    BeanUtils.copyProperties(goodsSaleMaterial, detail, "id");
                    if(goodsSale!=null){
                        detail.setProId(goodsSale.getProjectId());
                        detail.setProNo(goodsSale.getProjectNo());
                        detail.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    detail.setUseCount(lvRukumingxi.getUseCount());
                    detail.setGoodsType("0");//0：物料，1：产品
                    detail.setRukuId(lvRukumingxi.getId());
                    detail.setRukuNos(lvRukumingxi.getRukuno());
                    detail.setSupplierId(lvRukumingxi.getSupplierId());
                    detail.setSupplierName(lvRukumingxi.getSupplierName());
                    detail.setUserId(entity.getUserId());
                    detail.setUserName(entity.getUserName());
                    detail.setUseDate(entity.getUseDate());
                    detail.setMaterialId(entity.getMaterialId());
                    detail.setStatus(BusinessConstants.GOODS_METAIL_0);//工人领料
                    detail.setFactoryGoodssaleId(entity.getSaleAllId());
                    detail.setFactoryProdNo(entity.getProduceNo());
                    detail.setFactoryProdId(entity.getProduceId());

                    detail.setGoodSaleId(lvRukumingxi.getGoodsId());
                    detail.setGoodCode(lvRukumingxi.getCode());
                    if(lvRukumingxi.getParams().get("goodsName")!=null){
                        detail.setGoodName(lvRukumingxi.getParams().get("goodsName").toString());
                    }
                    if(lvRukumingxi.getParams().get("goodsName")!=null) {
                        detail.setGoodXinghao(lvRukumingxi.getParams().get("goodsName").toString());
                    }
                    iGoodsSaleMaterialDetailService.insert(detail);
                    usedCount=usedCount.add(lvRukumingxi.getUseCount());
                }
            }
            goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().add(usedCount));
            goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getGoodsCount().subtract(goodsSaleMaterial.getLingquCount()));
            iGoodsSaleMaterialService.update(goodsSaleMaterial);
            return 1;
        }else{
            return 0;
        }
    }

    //根据产品领料
    @Override
    public int submitSale(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        if (!rows.isEmpty()) {
            FactoryProduceSaleAll goodsSale=iFactoryProduceSaleAllService.selectById(entity.getSaleAllId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String time = sdf.format(new Date());
                lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                lvRukumingxiService.update(lvRukumingxi);

                LvRukumingxi lvRukumingxiChuku=new LvRukumingxi();
                BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiChuku, "id");
                lvRukumingxiChuku.setMingxiId(lvRukumingxi.getId());
                lvRukumingxiChuku.setDaohuoCount(lvRukumingxi.getUseCount());
                lvRukumingxiChuku.setCanCount(BigDecimal.ZERO);
                lvRukumingxiChuku.setRukudate(new Date());
                lvRukumingxiChuku.setRukuno(time);
                lvRukumingxiChuku.setRukuren(SecurityUtils.getLoginUser().getUsername());
                lvRukumingxiChuku.setType(BusinessConstants.TYPR_CHUKU);
                lvRukumingxiChuku.setFactoryGoodssaleId(entity.getSaleAllId());
                lvRukumingxiChuku.setFactoryProdNo(entity.getProduceNo());
                lvRukumingxiChuku.setFactoryProdId(entity.getProduceId());
                if(goodsSale!=null){
                    lvRukumingxiChuku.setProjectId(goodsSale.getProjectId());
                    lvRukumingxiChuku.setProjectNo(goodsSale.getProjectNo());
                    lvRukumingxiChuku.setFactoryGoodssaleId(goodsSale.getId());
                }
                if(lvRukumingxi.getGoodsBuyTotal()!=null){
                    LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
                    lvRukumingxiChuku.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().subtract(lvRukumingxi.getGoodsBuyTotal()));
                }
                lvRukumingxiService.insert(lvRukumingxiChuku);
                GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(lvRukumingxiChuku.getGoodsId());
                goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                goodsOrderInService.update(goodsOrderIn);

                if(goodsSale!=null){
                    detail.setProjectId(goodsSale.getProjectId());
                    detail.setProjectNo(goodsSale.getProjectNo());
                    detail.setFactoryGoodssaleId(goodsSale.getId());
                }
                detail.setProId(lvRukumingxi.getProjectId());
                detail.setProNo(lvRukumingxi.getProjectNo());
                detail.setGoodsId(goodsSale.getGoodsId());
                detail.setGoodsCode(goodsSale.getCode());
                detail.setGoodsName(goodsSale.getName());
                detail.setGoodModel(goodsSale.getXinghao());
                detail.setGoodsCount(goodsSale.getCount());
                detail.setGsaleId(goodsSale.getId());
                detail.setUseCount(lvRukumingxi.getUseCount());
                detail.setGoodsType("0");//0：物料，1：产品
                detail.setRukuId(lvRukumingxi.getId());
                detail.setRukuNos(lvRukumingxi.getRukuno());
                detail.setSupplierId(lvRukumingxi.getSupplierId());
                detail.setSupplierName(lvRukumingxi.getSupplierName());
                detail.setUserId(entity.getUserId());
                detail.setUserName(entity.getUserName());
                detail.setUseDate(entity.getUseDate());
                //detail.setMaterialId(entity.getMaterialId());
                detail.setStatus(BusinessConstants.GOODS_METAIL_0);//工人领料
                detail.setFactoryGoodssaleId(entity.getSaleAllId());
                detail.setFactoryProdNo(entity.getProduceNo());
                detail.setFactoryProdId(entity.getProduceId());

                detail.setGoodSaleId(lvRukumingxi.getGoodsId());
                detail.setGoodCode(lvRukumingxi.getCode());
                if(lvRukumingxi.getParams().get("goodsName")!=null){
                    detail.setGoodName(lvRukumingxi.getParams().get("goodsName").toString());
                }
                if(lvRukumingxi.getParams().get("goodsName")!=null) {
                    detail.setGoodXinghao(lvRukumingxi.getParams().get("goodsName").toString());
                }
                iGoodsSaleMaterialDetailService.insert(detail);
            }
            return 1;
        }else{
            return 0;
        }
    }
    //领料单领料
    @Override
    public int submitMain(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        BigDecimal usedCount=BigDecimal.ZERO;
        if (!rows.isEmpty()) {
            GoodsSaleMaterialChild goodsSaleMaterialChild=iGoodsSaleMaterialChildService.selectById(entity.getMaterialChildId());
            FactoryProduceSaleAll goodsSale=iFactoryProduceSaleAllService.selectById(goodsSaleMaterialChild.getGsaleId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());
                if(goodsSaleMaterialChild!=null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String time = sdf.format(new Date());
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                    LvRukumingxi lvRukumingxiChuku=new LvRukumingxi();
                    BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiChuku, "id");
                    lvRukumingxiChuku.setParentId(lvRukumingxi.getId());
                    lvRukumingxiChuku.setDaohuoCount(lvRukumingxi.getUseCount());
                    lvRukumingxiChuku.setCanCount(BigDecimal.ZERO);
                    lvRukumingxiChuku.setRukudate(new Date());
                    lvRukumingxiChuku.setRukuno(time);
                    lvRukumingxiChuku.setRukuren(SecurityUtils.getLoginUser().getUsername());
                    lvRukumingxiChuku.setType(BusinessConstants.TYPR_CHUKU);
                    lvRukumingxiChuku.setFactoryGoodssaleId(entity.getSaleAllId());
                    lvRukumingxiChuku.setFactoryProdNo(entity.getProduceNo());
                    lvRukumingxiChuku.setFactoryProdId(entity.getProduceId());
                    if(goodsSale!=null){
                        lvRukumingxiChuku.setProjectId(goodsSale.getProjectId());
                        lvRukumingxiChuku.setProjectNo(goodsSale.getProjectNo());
                        lvRukumingxiChuku.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    /*if(lvRukumingxi.getGoodsBuyTotal()!=null){
                        LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
                        lvRukumingxiChuku.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().subtract(lvRukumingxi.getGoodsBuyTotal()));
                    }*/
                    lvRukumingxiChuku.setPsaleId(goodsSaleMaterialChild.getId());
                    lvRukumingxiService.insert(lvRukumingxiChuku);
                    GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(lvRukumingxiChuku.getGoodsId());
                    goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderInService.update(goodsOrderIn);

                    BeanUtils.copyProperties(goodsSaleMaterialChild , detail, "id");
                    if(goodsSale!=null){
                        detail.setProId(goodsSale.getProjectId());
                        detail.setProNo(goodsSale.getProjectNo());
                        detail.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    detail.setUseCount(lvRukumingxi.getUseCount());
                    detail.setGoodsType("0");//0：物料，1：产品
                    detail.setRukuId(lvRukumingxi.getId());
                    detail.setRukuNos(lvRukumingxi.getRukuno());
                    detail.setSupplierId(lvRukumingxi.getSupplierId());
                    detail.setSupplierName(lvRukumingxi.getSupplierName());
                    detail.setUserId(entity.getUserId());
                    detail.setUserName(entity.getUserName());
                    detail.setUseDate(entity.getUseDate());
                    detail.setMaterialId(entity.getMaterialId());
                    detail.setStatus(BusinessConstants.GOODS_METAIL_0);//工人领料
                    detail.setFactoryGoodssaleId(entity.getSaleAllId());
                    detail.setFactoryProdNo(entity.getProduceNo());
                    detail.setFactoryProdId(entity.getProduceId());
                    detail.setParentId(goodsSaleMaterialChild.getId());

                    detail.setGoodSaleId(lvRukumingxi.getGoodsId());
                    detail.setGoodCode(lvRukumingxi.getGoodsCode());
                    detail.setGoodName(lvRukumingxi.getGoodsName());
                    detail.setGoodXinghao(lvRukumingxi.getGoodsModel());
                    iGoodsSaleMaterialDetailService.insert(detail);
                    usedCount=usedCount.add(lvRukumingxi.getUseCount());
                }
            }
            goodsSaleMaterialChild.setLingquCount(goodsSaleMaterialChild.getLingquCount().add(usedCount));
            goodsSaleMaterialChild.setShengyuCount(goodsSaleMaterialChild.getGoodsCount().subtract(goodsSaleMaterialChild.getLingquCount()));
            iGoodsSaleMaterialChildService.update(goodsSaleMaterialChild);
            GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(goodsSaleMaterialChild.getMaterialdId());
            goodsSaleMaterial.setShijicount(goodsSaleMaterial.getShijicount().add(usedCount));
            iGoodsSaleMaterialService.update(goodsSaleMaterial);
            return 1;
        }else{
            return 0;
        }
    }
    //其他出入库选用产品
    @Override
    public int submitOther(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        BigDecimal usedCount=BigDecimal.ZERO;
        if (!rows.isEmpty()) {
            GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(entity.getMaterialId());
            FactoryProduceSaleAll goodsSale=iFactoryProduceSaleAllService.selectById(entity.getSaleAllId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());
                if(goodsSaleMaterial!=null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String time = sdf.format(new Date());
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                    LvRukumingxi lvRukumingxiChuku=new LvRukumingxi();
                    BeanUtils.copyProperties(lvRukumingxi, lvRukumingxiChuku, "id");
                    lvRukumingxiChuku.setParentId(lvRukumingxi.getId());
                    lvRukumingxiChuku.setDaohuoCount(lvRukumingxi.getUseCount());
                    lvRukumingxiChuku.setCanCount(BigDecimal.ZERO);
                    lvRukumingxiChuku.setRukudate(new Date());
                    lvRukumingxiChuku.setRukuno(time);
                    lvRukumingxiChuku.setRukuren(SecurityUtils.getLoginUser().getUsername());
                    lvRukumingxiChuku.setType(BusinessConstants.TYPR_CHUKU);
                    lvRukumingxiChuku.setFactoryGoodssaleId(entity.getSaleAllId());
                    lvRukumingxiChuku.setFactoryProdNo(entity.getProduceNo());
                    lvRukumingxiChuku.setFactoryProdId(entity.getProduceId());
                    if(goodsSale!=null){
                        lvRukumingxiChuku.setProjectId(goodsSale.getProjectId());
                        lvRukumingxiChuku.setProjectNo(goodsSale.getProjectNo());
                        lvRukumingxiChuku.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    if(lvRukumingxi.getGoodsBuyTotal()!=null){
                        LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
                        lvRukumingxiChuku.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().subtract(lvRukumingxi.getGoodsBuyTotal()));
                    }
                    lvRukumingxiService.insert(lvRukumingxiChuku);
                    GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(lvRukumingxiChuku.getGoodsId());
                    goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(lvRukumingxiChuku.getDaohuoCount()));
                    goodsOrderInService.update(goodsOrderIn);

                    BeanUtils.copyProperties(goodsSaleMaterial, detail, "id");
                    if(goodsSale!=null){
                        detail.setProId(goodsSale.getProjectId());
                        detail.setProNo(goodsSale.getProjectNo());
                        detail.setFactoryGoodssaleId(goodsSale.getId());
                    }
                    detail.setUseCount(lvRukumingxi.getUseCount());
                    detail.setGoodsType("0");//0：物料，1：产品
                    detail.setRukuId(lvRukumingxi.getId());
                    detail.setRukuNos(lvRukumingxi.getRukuno());
                    detail.setSupplierId(lvRukumingxi.getSupplierId());
                    detail.setSupplierName(lvRukumingxi.getSupplierName());
                    detail.setUserId(entity.getUserId());
                    detail.setUserName(entity.getUserName());
                    detail.setUseDate(entity.getUseDate());
                    detail.setMaterialId(entity.getMaterialId());
                    detail.setStatus(BusinessConstants.GOODS_METAIL_0);//工人领料
                    detail.setFactoryGoodssaleId(entity.getSaleAllId());
                    detail.setFactoryProdNo(entity.getProduceNo());
                    detail.setFactoryProdId(entity.getProduceId());

                    detail.setGoodSaleId(lvRukumingxi.getGoodsId());
                    detail.setGoodCode(lvRukumingxi.getGoodsCode());
                    detail.setGoodName(lvRukumingxi.getGoodsName());
                    detail.setGoodXinghao(lvRukumingxi.getGoodsModel());
                    iGoodsSaleMaterialDetailService.insert(detail);
                    usedCount=usedCount.add(lvRukumingxi.getUseCount());
                }
            }
            goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().add(usedCount));
            goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getGoodsCount().subtract(goodsSaleMaterial.getLingquCount()));
            iGoodsSaleMaterialService.update(goodsSaleMaterial);
            return 1;
        }else{
            return 0;
        }
    }

    //锁定产品
    @Override
    public int submitGoodsSale(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        BigDecimal usedCount=BigDecimal.ZERO;
        if (!rows.isEmpty()) {
           // GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(entity.getMaterialId());
            GoodsSale goodsSale=goodsSaleService.selectById(entity.getGsaleId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());
                if(goodsSale!=null) {
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                    BeanUtils.copyProperties(lvRukumingxi, detail, "id");
                    detail.setGoodsCode(lvRukumingxi.getCode());
                    detail.setGoodsName(goodsSale.getGoodsName());
                    detail.setGoodModel(lvRukumingxi.getGoodsModel());
                    detail.setGoodCode(goodsSale.getGoodsCode());
                    detail.setGoodXinghao(goodsSale.getModel());
                    detail.setGsaleId(lvRukumingxi.getGoodsSaleId());
                    detail.setGoodsCount(goodsSale.getOrderCount());
                    detail.setGoodsUnit(entity.getGoodsUnit());
                    detail.setGoodsType("1");//0：物料，1：产品
                    detail.setProId(goodsSale.getProjectId());
                    detail.setProNo(goodsSale.getProjectNo());
                    detail.setGoodSaleId(goodsSale.getId());
                    detail.setUseCount(lvRukumingxi.getUseCount());
                    detail.setRukuId(lvRukumingxi.getId());
                    detail.setRukuNos(lvRukumingxi.getRukuno());
                    detail.setSupplierId(lvRukumingxi.getSupplierId());
                    detail.setSupplierName(lvRukumingxi.getSupplierName());
                    detail.setUserId(entity.getUserId());
                    detail.setUserName(entity.getUserName());
                    detail.setUseDate(entity.getUseDate());
                    detail.setMaterialId(entity.getMaterialId());
                    detail.setStatus(BusinessConstants.GOODS_METAIL_1);//锁定产品
                    iGoodsSaleMaterialDetailService.insert(detail);
                    usedCount=usedCount.add(lvRukumingxi.getUseCount());
                }
            }
            /*goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().add(usedCount));
            goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getGoodsCount().subtract(goodsSaleMaterial.getLingquCount()));
            iGoodsSaleMaterialService.update(goodsSaleMaterial);*/
            return 1;
        }else{
            return 0;
        }
    }

    //发货选定产品
    @Override
    public int submitGoodsSaleFahuo(GoodsSaleMaterialDetail entity) {
        List<LvRukumingxi> rows = entity.getColumns();
        BigDecimal usedCount=BigDecimal.ZERO;
        if (!rows.isEmpty()) {
            // GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(entity.getMaterialId());
            GoodsSale goodsSale=goodsSaleService.selectById(entity.getGsaleId());
            for (LvRukumingxi lvRukumingxi : rows) {
                GoodsSaleMaterialDetail detail = new GoodsSaleMaterialDetail();
                detail.setId(IdUtils.fastSimpleUUID());
                if(goodsSale!=null) {
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().subtract(lvRukumingxi.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                    BeanUtils.copyProperties(lvRukumingxi, detail, "id");
                    detail.setGoodsCode(lvRukumingxi.getCode());
                    detail.setGoodsName(goodsSale.getGoodsName());
                    detail.setGoodModel(lvRukumingxi.getGoodsModel());
                    detail.setGoodCode(goodsSale.getGoodsCode());
                    detail.setGoodXinghao(goodsSale.getModel());
                    detail.setGsaleId(lvRukumingxi.getGoodsSaleId());
                    detail.setGoodsCount(goodsSale.getOrderCount());
                    detail.setGoodsUnit(entity.getGoodsUnit());
                    detail.setGoodsType("1");//0：物料，1：产品
                    detail.setProId(goodsSale.getProjectId());
                    detail.setProNo(goodsSale.getProjectNo());
                    detail.setGoodSaleId(goodsSale.getId());
                    detail.setUseCount(lvRukumingxi.getUseCount());
                    detail.setRukuId(lvRukumingxi.getId());
                    detail.setRukuNos(lvRukumingxi.getRukuno());
                    detail.setSupplierId(lvRukumingxi.getSupplierId());
                    detail.setSupplierName(lvRukumingxi.getSupplierName());
                    detail.setUserId(entity.getUserId());
                    detail.setUserName(entity.getUserName());
                    detail.setUseDate(entity.getUseDate());
                    detail.setMaterialId(entity.getMaterialId());
                    detail.setYifahuomingxiId(entity.getYifahuomingxiId());
                    detail.setStatus(BusinessConstants.GOODS_METAIL_2);//锁定产品
                    iGoodsSaleMaterialDetailService.insert(detail);
                    usedCount=usedCount.add(lvRukumingxi.getUseCount());
                }
            }
            /*goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().add(usedCount));
            goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getGoodsCount().subtract(goodsSaleMaterial.getLingquCount()));
            iGoodsSaleMaterialService.update(goodsSaleMaterial);*/
            return 1;
        }else{
            return 0;
        }
    }
    /**
     * 修改销售产品物料选用明细
     * @param entity 销售产品物料选用明细
     * @return 结果
     */
    @Override
    public int update(GoodsSaleMaterialDetail entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaleMaterialDetailMapper.update(entity);
    }

    /**
     * 批量删除销售产品物料选用明细
     * @param ids 需要删除的销售产品物料选用明细主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        for(String id:ids){
            GoodsSaleMaterialDetail goodsSaleMaterialDetail=goodsSaleMaterialDetailMapper.selectById(id);
            if(goodsSaleMaterialDetail!=null){
                //领料主表回复领取和剩余数量
                GoodsSaleMaterial goodsSaleMaterial=iGoodsSaleMaterialService.selectById(goodsSaleMaterialDetail.getMaterialId());
                if(goodsSaleMaterial!=null){
                    goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().subtract(goodsSaleMaterialDetail.getUseCount()));
                    goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getShengyuCount().add(goodsSaleMaterialDetail.getUseCount()));
                    iGoodsSaleMaterialService.update(goodsSaleMaterial);
                    //删除领料出库记录
                    lvRukumingxiService.deleteById(goodsSaleMaterialDetail.getRukuId());
                    GoodsOrderIn goodsOrderIn=goodsOrderInService.selectById(goodsSaleMaterialDetail.getGoodsId());
                    //恢复库存主表用料情况
                    goodsOrderIn.setCount(goodsOrderIn.getCount().add(goodsSaleMaterialDetail.getUseCount()));
                    goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(goodsSaleMaterialDetail.getUseCount()));
                    goodsOrderInService.update(goodsOrderIn);
                }
                LvRukumingxi lvRukumingxi=lvRukumingxiService.selectById(goodsSaleMaterialDetail.getRukuId());
                if(lvRukumingxi!=null){
                    lvRukumingxi.setCanCount(lvRukumingxi.getCanCount().add(goodsSaleMaterialDetail.getUseCount()));
                    lvRukumingxiService.update(lvRukumingxi);
                }
            }

        }
        return goodsSaleMaterialDetailMapper.deleteByIds(ids);
    }

    /**
     * 删除销售产品物料选用明细信息
     * @param id 销售产品物料选用明细主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSaleMaterialDetailMapper.deleteById(id);
    }
}
